package Test_Web;

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * Servlet implementation class BrowseByTitle
 */
public class BrowseByTitle extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public BrowseByTitle() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doPost(request, response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// begin to build HTML page
		response.setContentType("text/html");

        PrintWriter out = response.getWriter();

        out.println("<HTML><HEAD><TITLE>List By Title</TITLE></HEAD>");
        out.println("<BODY><H1>List By Title</H1>");
		
		// build sql command to list all genres
		try
		{
			try {
				Class.forName("com.mysql.jdbc.Driver").newInstance();
			} catch (Exception e) {
				e.printStackTrace();
			}
			Connection connect;
			connect = DriverManager.getConnection("jdbc:mysql:///moviedb","testuser","testpass");
			Statement tempStatement = connect.createStatement();
			String command = "select substr(title,1,1) from movies group by substr(title,1,1)";
		
			// execute query
			ResultSet resultSet = tempStatement.executeQuery(command);
			
			out.println("<TABLE border>");
			
			while(resultSet.next())
			{
				String title = resultSet.getString("substr(title,1,1)");
				out.println("<tr>" +
                        "<td><a href=\"BrowseBySingleTitle" +
                        "?title=" + title +//Form data
                        "\">" + title + "</td>" +
                        "</a></tr>");
			}
			 out.println("</TABLE>");
			 
			// Shopping Cart and Main Page Link
 			out.println("<br><br>");
 			out.println("<a href = 'ShoppingCart'>Shopping Cart</a> | " + 
 					"<a href = 'MainPage.jsp'>Main Page</a>");
	 			
             resultSet.close();
             tempStatement.close();
             connect.close();
         	 out.close();
		}
		catch (SQLException ex) {
            while (ex != null) {
                  System.out.println ("SQL Exception:  " + ex.getMessage ());
                  ex = ex.getNextException ();
              }  // end while
          }  // end catch SQLException

      catch(java.lang.Exception ex)
          {
              out.println("<HTML>" +
                          "<HEAD><TITLE>" +
                          "MovieDB: Error" +
                          "</TITLE></HEAD>\n<BODY>" +
                          "<P>SQL error in doGet: " +
                          ex.getMessage() + "</P></BODY></HTML>");
              return;
          }
	}
}
